package org.example.admin.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.admin.dto.TableUser;
import org.example.admin.service.AddressService;
import org.example.admin.service.CartService;
import org.example.admin.service.UserService;
import org.example.admin.utils.ConditionWithQuery;
import org.example.entity.Address;
import org.example.entity.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController extends ApiController {

    @Resource
    private UserService userService;

    @Resource
    private AddressService addressService;

    @Resource
    private CartService cartService;

    /**
     * 展示+模糊查询普通用户表
     * @param user
     * @param pageNumber
     * @param pageSize
     * @param req
     * @return
     */
    @RequestMapping("/list")
    public String userList(User user, String pageNumber, String pageSize, HttpServletRequest req) {

        ConditionWithQuery query = new ConditionWithQuery();
        QueryWrapper<User> queryWrapper = query.withQuery(req);

        //分页 pageNumber--》页数    pageSize--》每页显示数据的条数
        long pNum = Long.parseLong(pageNumber);
        long pSize = Long.parseLong(pageSize);
        Page<User> page1 = this.userService.page(new Page<>(pNum, pSize), queryWrapper);

        return JSON.toJSONString(new TableUser<>(page1.getRecords(), (int) page1.getTotal()));
    }

    /**
     * 查看收货地址
     * @param uId
     * @return
     */
    @RequestMapping("/showAddress")
    public String showAddress(String uId) {

        System.out.println(uId);

        Integer userId = Integer.parseInt(uId);
        Address address = new Address();
        address.setUId(userId);
        List<Address> list = this.addressService.list(new QueryWrapper<>(address));

        return JSON.toJSONString(list);
    }

    /**
     * 查看购物车信息
     * @param uId
     * @return
     */
    @RequestMapping("/showShoppingCart")
    public String showShoppingCart(String uId) {
        return JSON.toJSONString(cartService.getSelectCart(Integer.valueOf(uId)));
    }


    /**
     * 修改用户状态
     * @param uId
     * @param uStatus
     * @return
     */
    @RequestMapping("/activate")
    public int activate(String uId, String uStatus) {
        int newStatus = Integer.parseInt(uStatus) == 1 ? 0 : 1;
        return userService.updateByuId(Integer.parseInt(uId),newStatus);
    }
}
















